﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Collections.ObjectModel;

namespace Nok_Air
{
    public partial class Confirm : PhoneApplicationPage
    {
        private string indexAvailabilityItemsPar = "";
        private string lastnamePar = "";
        private string roundtripFlag = "";
        private int[] indexAvailabilityItems;
        private string[] sellingClass_indexAvailabilityItems;

        private string FlightsXMLStringPar = "";
        private string RegisterIdPar = "";
        private string PaymentTypePar = "";
        private string PassengerXMLPar = "";
        private string ContactXMLPar = "";
        public Confirm()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.SubmitBookingV2Completed += (s1, e1) =>
            {
                if (e1.exception == null)
                {
                    nokSHelp.GetSubmitBookingStatusCompleted += (s, ev) =>
                    {
                        if (ev.exception == null)
                            Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/BookFlight/8Finish.xaml", UriKind.Relative)) );
                        else
                            Dispatcher.BeginInvoke(() => MessageBox.Show(ev.exception.Message) );
                    };
                }
                else
                    Dispatcher.BeginInvoke(() => MessageBox.Show(e1.exception.Message));
            };
            nokSHelp.SubmitBookingV2(App.ViewModel.PricingV2Items.PricingID,
                FlightsXMLStringPar, PassengerXMLPar, PaymentTypePar, App.ViewModel.PricingV2Items.TotalAmount, ContactXMLPar, RegisterIdPar, lastnamePar);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationService.CanGoBack && App.ViewModel.IsFinishTransaction)
            {
                NavigationService.GoBack();
            }

            NavigationContext.QueryString.TryGetValue("lastnamePar", out lastnamePar);

            NavigationContext.QueryString.TryGetValue("FlightsXMLStringPar", out FlightsXMLStringPar);
            NavigationContext.QueryString.TryGetValue("RegisterIdPar", out RegisterIdPar);
            NavigationContext.QueryString.TryGetValue("PaymentTypePar", out PaymentTypePar);
            NavigationContext.QueryString.TryGetValue("PassengerXMLPar", out PassengerXMLPar);
            NavigationContext.QueryString.TryGetValue("ContactXMLPar", out ContactXMLPar);


            if (NavigationContext.QueryString.TryGetValue("roundtripFlag", out roundtripFlag))
            {
                //default is Oneway=> 0==oneway,1==roundTrip
                if (roundtripFlag == "0")
                {
                    indexAvailabilityItems = new int[1];
                    sellingClass_indexAvailabilityItems = new string[1];
                }
                else
                {
                    indexAvailabilityItems = new int[2];
                    sellingClass_indexAvailabilityItems = new string[2];
                }

                for (int aitemIndex = 0; aitemIndex < indexAvailabilityItems.Count(); aitemIndex++)
                {
                    indexAvailabilityItems[aitemIndex] = -1;
                    sellingClass_indexAvailabilityItems[aitemIndex] = "-1";
                }
            }

            if (NavigationContext.QueryString.TryGetValue("indexAvailabilityItems", out indexAvailabilityItemsPar))
            {
                if (indexAvailabilityItemsPar.Contains(','))
                {
                    string[] tmpPar = indexAvailabilityItemsPar.Split(',').ToArray();
                    for (int i = 0; i < indexAvailabilityItems.Count(); i++)
                    {
                        indexAvailabilityItems[i] = int.Parse(tmpPar[i].Split('_')[0]);
                        sellingClass_indexAvailabilityItems[i] = tmpPar[i].Split('_')[1];
                    }
                }
                else
                {
                    indexAvailabilityItems[0] = int.Parse(indexAvailabilityItemsPar.Split('_')[0]);
                    sellingClass_indexAvailabilityItems[0] = indexAvailabilityItemsPar.Split('_')[1];
                }
            }

            //    List<AvailabilityItemViewModel> _availabilityItem = new List<AvailabilityItemViewModel>(App.ViewModel.AvailabilityItems.Count());
            //    _availabilityItem.Add(App.ViewModel.AvailabilityItems[0]);

            App.ViewModel.FlightSegmentItems = new ObservableCollection<FlightSegmentItemViewModel>();
            for (int i = 0; i < indexAvailabilityItems.Count(); i++)
            {
                App.ViewModel.FlightSegmentItems.Add(App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]]);
            }

            /*
            TextBlock tb = new TextBlock();

            for (int i = 0; i < indexAvailabilityItems.Count(); i++)
            {
                tb.Text = App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureAirport + Environment.NewLine;
                tb.Text += App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalAirport + Environment.NewLine;
                tb.Text += App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].BoardDate + Environment.NewLine;
                tb.Text += App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureTime + Environment.NewLine;
                tb.Text += App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].FlightNumber + Environment.NewLine;
            }
            ReportPanel.Children.Add(tb);
            */
        }
    }
}
